package com.japancompany.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.japancompany.entity.Article;
import com.japancompany.mapper.ArticleMapper;
import com.japancompany.service.ArticleService;
import com.japancompany.service.CategoryService;
import com.japancompany.service.UserService;
import com.japancompany.common.PageQuery;
import com.japancompany.common.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Override
    public PageResult<Article> getArticlePage(PageQuery pageQuery, String title, Long categoryId, Integer status) {
        Page<Article> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        
        if (title != null && !title.trim().isEmpty()) {
            queryWrapper.like("title", title);
        }
        
        if (categoryId != null) {
            queryWrapper.eq("category_id", categoryId);
        }
        
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        
        queryWrapper.orderByDesc("is_top", "create_time");
        
        Page<Article> result = page(page, queryWrapper);
        
        return new PageResult<>(result.getTotal(), result.getRecords(), 
                (int) result.getCurrent(), (int) result.getSize());
    }

    @Override
    public Article getArticleDetail(Long id) {
        Article article = getById(id);
        if (article != null) {
            incrementViewCount(id);
        }
        return article;
    }

    @Override
    @Transactional
    public boolean createArticle(Article article) {
        if (article.getCategoryId() != null) {
            var category = categoryService.getById(article.getCategoryId());
            if (category != null) {
                article.setCategoryName(category.getCategoryName());
            }
        }
        
        if (article.getAuthorId() != null) {
            var author = userService.getById(article.getAuthorId());
            if (author != null) {
                article.setAuthorName(author.getRealName());
            }
        }
        
        article.setViewCount(0);
        article.setLikeCount(0);
        article.setCommentCount(0);
        
        return save(article);
    }

    @Override
    @Transactional
    public boolean updateArticle(Article article) {
        Article existingArticle = getById(article.getId());
        if (existingArticle == null) {
            throw new RuntimeException("文章不存在");
        }
        
        if (article.getCategoryId() != null) {
            var category = categoryService.getById(article.getCategoryId());
            if (category != null) {
                article.setCategoryName(category.getCategoryName());
            }
        }
        
        if (article.getAuthorId() != null) {
            var author = userService.getById(article.getAuthorId());
            if (author != null) {
                article.setAuthorName(author.getRealName());
            }
        }
        
        return updateById(article);
    }

    @Override
    public boolean deleteArticle(Long id) {
        return removeById(id);
    }

    @Override
    public boolean updateArticleStatus(Long id, Integer status) {
        Article article = new Article();
        article.setId(id);
        article.setStatus(status);
        return updateById(article);
    }

    @Override
    public boolean incrementViewCount(Long id) {
        Article article = getById(id);
        if (article != null) {
            article.setViewCount(article.getViewCount() + 1);
            return updateById(article);
        }
        return false;
    }

    @Override
    public boolean incrementLikeCount(Long id) {
        Article article = getById(id);
        if (article != null) {
            article.setLikeCount(article.getLikeCount() + 1);
            return updateById(article);
        }
        return false;
    }

    @Override
    public List<Article> getLatestArticles() {
        return list(new QueryWrapper<Article>()
                .eq("deleted", 0)
                .eq("status", 1)
                .orderByDesc("create_time")
                .last("LIMIT 6"));
    }

    @Override
    public List<Article> getRecommendArticles() {
        return list(new QueryWrapper<Article>()
                .eq("deleted", 0)
                .eq("status", 1)
                .eq("is_recommend", 1)
                .orderByDesc("create_time")
                .last("LIMIT 5"));
    }

    @Override
    public List<Article> getTopArticles() {
        return list(new QueryWrapper<Article>()
                .eq("deleted", 0)
                .eq("status", 1)
                .eq("is_top", 1)
                .orderByDesc("create_time")
                .last("LIMIT 5"));
    }
}